/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * http://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */
package com.dian.modules.base.service.impl;

import com.dian.common.utils.DateUtils;
import com.dian.common.validator.ValidatorUtils;
import com.dian.common.validator.group.AddGroup;
import com.dian.common.validator.group.UpdateGroup;
import com.dian.common.utils.BeanConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import cn.hutool.core.util.StrUtil;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.dian.common.utils.PageUtils;
import com.dian.common.utils.Query;
import com.dian.common.server.CommonService;
import com.dian.modules.base.dao.MemberDao;
import com.dian.modules.base.entity.MemberEntity;
import com.dian.modules.base.service.MemberService;
import com.dian.modules.base.vo.MemberExportVO;

/**
 * 职员服务实现类
 *
 * @author zhourui
 * @email 1436907200@qq.com
 * @date 2019-07-04 15:53:32
 */
@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    public CommonService commonService;


    /**
     * 职员分页
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<MemberEntity> page = this.page(new Query<MemberEntity>().getPage(params),getQueryWrapper(params) );
        return new PageUtils(page);
    }

    /**
     *  职员新增
     * @param memberEntity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean saveInfo(MemberEntity memberEntity) {

        //设置编码，等基础默然初始化数据设置
        //数据完整性校验
        this.paramsCheck(memberEntity,AddGroup.class);

        //保存
        this.save(memberEntity);

        return true;
    }

    /**
     *职员更新
     * @param memberEntity
     * @return
     */
    @Override
    public boolean updateInfo(MemberEntity memberEntity) {

        //修改状态校验
        this.updateCheck(memberEntity.getId());

        //主表数据完整性校验
        this.paramsCheck(memberEntity, UpdateGroup.class);

        //更新主表
        this.updateById(memberEntity);


        return true;
    }

    /**
     *职员删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteInfo(Long[] ids) {

        //删除状态校验
        this.deleteCheck(ids);

        //更新主表
        this.remove(new QueryWrapper<MemberEntity>().in("id",ids));

        return true;
    }

    /**
     * 职员详情
     * @param id
     * @return
     */
    @Override
    public MemberEntity getInfo(Long id) {
        MemberEntity memberEntity =getById(id);
        return memberEntity;
    }




    /**
     *职员当前页or全部导出
     * @param params
     * @return
     */
    @Override
    public List<MemberExportVO> exportList(Map<String, Object> params) {
        List<MemberEntity> list;
        if(!StrUtil.isBlankIfStr(params.get("exprotType"))){
            list= this.page(new Query<MemberEntity>().getPage(params),getQueryWrapper(params)).getRecords();
        }else{
            list= this.list(getQueryWrapper(params));
        }

        List<MemberExportVO> resultList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)) {
            resultList = BeanConverter.convertList(list, MemberExportVO.class);
        }
        return resultList;
    }



    /***********************************************************************************************/
    /****************************************** 私有方法 ********************************************/
    /***********************************************************************************************/

    /**
     * 修改状态校验
     *
     * @param
     */
    private void updateCheck(Long id) {
        MemberEntity memberEntity =this.getById(id);
        //if(memberEntity.getOrderState().equals(OrderHead_OrderStateEnum.AUDITED.getValue())){
        //    throw new RRException(String.format("已审核的销售订单禁止修改"));
        //}
    }

    /**
     * 删除状态校验
     *
     * @param ids
     */
    private void deleteCheck(Long[] ids) {
        for (Long id:ids) {
                MemberEntity memberEntity = this.getById(id);
        }
        //if(!saleOrderHeadEntity.getOrderState().equals(OrderHead_OrderStateEnum.WAITCHECK.getValue())){
        //    throw new RRException(String.format("只允许删除未审核的销售订单"));
        //}
    }

    /**
     * 新增和修改参数校验
     *
     * @param record
     */
    private void paramsCheck(MemberEntity record, Class<?> cls) {
        ValidatorUtils.validateEntity(record, cls);
    }
    /**
     * 获取查询条件
     *
     * @param
     */
    private QueryWrapper<MemberEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<MemberEntity> queryWrapper=new QueryWrapper<>();
        if(!StrUtil.isBlankIfStr(params.get("memberCode"))){
            queryWrapper.like("member_code",params.get("memberCode"));
        }
        if(!StrUtil.isBlankIfStr(params.get("memberName"))){
            queryWrapper.like("member_name",params.get("memberName"));
        }
        if(!StrUtil.isEmptyIfStr(params.get("propKey"))){
            queryWrapper.and(wrapper -> wrapper
            );
        }

        queryWrapper.orderByAsc("id");
        return queryWrapper;
    }
}
